<template>
  <el-form :model="detailForm" :rules="rules" ref="detailForm"
           :label-position="labelPosition" :class="isEdit?'':'formItem'">
    <el-card class="box-card" shadow="never">
      <div slot="header" class="header-card">
        <span class="header-card-title">信息录入</span>
      </div>
      <el-row :gutter="24">
        <el-col :span="8" v-if="isShow">
          <el-form-item label="实际装饰开始时间:" prop="actualStartTime">
            <el-date-picker type="date" v-model="detailForm.actualStartTime" v-if="isEdit"
                            value-format='yyyy-MM-dd' style="width: 100%;" placeholder="选择实际装饰开始时间">
            </el-date-picker>
            <span v-else>{{ detailForm.actualStartTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="isShow">
          <el-form-item label="实际装饰结束时间:" prop="actualEndTime">
            <el-date-picker type="date" v-model="detailForm.actualEndTime" v-if="isEdit"
                            value-format='yyyy-MM-dd' style="width: 100%;" placeholder="选择实际装饰结束时间">
            </el-date-picker>
            <span v-else>{{ detailForm.actualEndTime }}</span>
          </el-form-item>
        </el-col>


        <el-col :span="8" v-if="isShow">
          <el-form-item label="是否个人垫付:" prop="feedbackAdvancePayment">
            <el-select v-model="detailForm.feedbackAdvancePayment" @change="handleSelect" v-if="isEdit"
                       placeholder="是否个人垫付"
                       style="width: 100%;">
              <el-option v-for="v in dict.type.advance_payment" :label="v.label" :key="v.value"
                         :value="Number(v.value)">
              </el-option>
            </el-select>
            <span v-else>
                {{ selectDictLabel(dict.type.advance_payment, detailForm.feedbackAdvancePayment) }}
            </span>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="isShow">
          <el-form-item label="付款类型:" prop="payType">
            <el-select v-model="detailForm.payType" v-if="isEdit" placeholder="请选择付款类型"
                       style="width: 100%;">
              <el-option v-for="v in dict.type.pay_type" :label="v.label" :key="v.value" :value="Number(v.value)">
              </el-option>
            </el-select>
            <span v-else>   {{ selectDictLabel(dict.type.pay_type, detailForm.payType) }} </span>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="24">
        <el-col :span="24" v-if="isShow">
          <el-form-item label="装饰完成照片:" prop="inspectionInvoiceUrl">
            <Image-common v-model="detailForm.inspectionInvoiceUrl" :disabled="!isEdit"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="24">
        <el-col :span="24" v-if="isShow">
          <el-form-item label="结算单上传:">
            <image-common v-model="detailForm.voucherUrl" :disabled="!isEdit" :limit="20" :upType="2"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="24">
        <el-col :span="24" v-if="isShow">
          <el-form-item label="装饰意见反馈:">
            <el-input v-if="isEdit" placeholder="请输入装饰意见反馈" type="textarea" :rows="4"
                      v-model="detailForm.view"></el-input>
            <span v-else style="margin:0;">{{ detailForm.view }}</span>
          </el-form-item>
        </el-col>
      </el-row>


    </el-card>
  </el-form>
</template>

<script>
import {effectDate} from "@/utils/libs/effectDate.js"

export default {
  name: 'InformationEntry',
  components: {
    LinkageSelect: () => import('../../../components/linkageSelect.vue'),
    ImgUpload: () => import('../../../components/ImgUpload')
  },
  dicts: ['pay_type', 'advance_payment'],
  model: {
    prop: 'ruleForm',
    event: 'update:ruleForm'
  },

  props: {
    ruleForm: {
      type: Object,
      default: () => {
        return {};
      },
    },
    state: {
      type: String,
      default: "",
    }
  },

  computed: {
    detailForm: {
      get() {
        let that = this
        const proxy = new Proxy(that.ruleForm, {
          get(target, key) {
            return Reflect.get(target, key)
          },
          set(target, key, value) {
            that.$emit('update:ruleForm', {...target, [key]: value})
            return true
          }
        })
        return proxy
      }
    },
    labelPosition() {
      if (this.isEdit) {
        return "top";
      } else {
        return "left";
      }
    },
    isEdit() {
      let bool;
      switch (this.state) {
        case "write":
          bool = true;
          break;
        case "disabled":
          bool = false;
          break;
        case "readonly":
          bool = false;
          break;
      }
      return bool;
    },
  },

  data() {
    var start = (rule, value, callback) => {
      let that = this
      if (value === '') {
        callback(new Error('实际装饰开始时间为空'));
      } else {
        if (that.detailForm.actualEndTime) {
          if (new Date(value) > new Date(that.detailForm.actualEndTime)) {
            callback(new Error('实际装饰开始时间大于实际装饰结束时间'));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }

    };
    var end = (rule, value, callback) => {
      let that = this
      if (value === '') {
        callback(new Error('实际装饰结束时间为空'));
      } else {
        if (new Date(value) < new Date(that.detailForm.actualStartTime)) {
          callback(new Error('实际装饰开始时间小于于实际装饰结束时间'));
        } else {
          callback();
        }
      }

    };
    return {
      isShow: true,
      rules: {
        actualStartTime: [{required: this.state === "write", validator: start, trigger: 'change'}],
        actualEndTime: [{required: this.state === "write", validator: end, trigger: 'change'}],
        feedbackAdvancePayment: [{required: this.state === "write", message: '请选择是否个人垫付', trigger: 'change'}],
        payType: [{required: this.state === "write", message: '请选择预选择付款类型', trigger: 'change'}],
        inspectionInvoiceUrl: [{required: this.state === "write", message: '请上传装饰完成照片', trigger: 'blur'}]
      }
    };
  },

  created() {
  },
  mounted() {
    this.handleClearValidate()
  },
  methods: {
    handleSelect(val) {
      this.detailForm.feedbackAdvancePayment = this.ruleForm.feedbackAdvancePayment = val
      this.detailForm.payType = this.ruleForm.payType = val
    },

    //表单效验
    handleEffect() {
      return new Promise((resolve, reject) => {
        this.$refs.detailForm.validate((valid) => {
          if (valid) {
            resolve()
          } else {
            reject()
          }
        });
      })
    },

    valid() {
      return new Promise((resolve) => {
        this.$refs.detailForm.validate((valid) => {
          resolve(valid);
        });
      })
    },

    //取消表单效验
    handleClearValidate() {
      this.$refs.detailForm.clearValidate()
    }
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/cwgd/formScss.scss";
</style>


